package com.kvest.sqlitetest.datastorage.sqlitestorage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.kvest.sqlitetest.datastorage.sqlitestorage.table.ProductTable;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 16.07.13
 * Time: 20:40
 * To change this template use File | Settings | File Templates.
 */
public class MultithreadingTestHelper extends SQLiteOpenHelper {
    private static final String TAG = "KVEST_TAG";

    private static final String DATABASE_NAME = "mutithreading_test_db";
    private static final int DATABASE_VERSION = 1;

    private SQLiteDatabase database;

    public MultithreadingTestHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        database = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //create all tables
        sqLiteDatabase.execSQL(ProductTable.CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    }

    public void close() {
        if (database != null && database.isOpen()) {
            database.close();
        }
    }

    public void clear() {
        if (!database.isOpen()) {
            Log.d(TAG, "PROBLEM WITH DB!");
            database = getWritableDatabase();
        }

        database.execSQL("DELETE FROM \"" + ProductTable.TABLE_NAME + "\" WHERE 1;");
    }

    public int getTableRowsCount(String tableName) {
        if (!database.isOpen()) {
            Log.d(TAG, "PROBLEM WITH DB!");
            database = getWritableDatabase();
        }

        int result = -1;
        Cursor cursor = database.rawQuery("SELECT count(*) FROM \"" + tableName + "\";", null);
        try
        {
            cursor.moveToFirst();
            result = cursor.getInt(0);
        } finally {
            cursor.close();
        }

        return result;
    }

    public void printTable(String tableName) {
        if (!database.isOpen()) {
            Log.d(TAG, "PROBLEM WITH DB!");
            database = getWritableDatabase();
        }
        Cursor cursor = database.rawQuery("SELECT * FROM \"" + tableName + "\";", null);
        Log.d(TAG, "Data from \"" + tableName + "\": ");
        Log.d(TAG, "--------------------------------------------");
        Log.d(TAG, getCursorHeader(cursor));
        Log.d(TAG, "--------------------------------------------");
        while (cursor.moveToNext()) {
            Log.d(TAG, getCursorData(cursor));
        }
        cursor.close();
        Log.d(TAG, "--------------------------------------------");
    }

    private static String getCursorHeader(Cursor cursor) {
        String result = "";
        for (int i = 0; i < cursor.getColumnCount(); ++i) {
            result += cursor.getColumnName(i) + "\t";
        }

        return result;
    }

    private static String getCursorData(Cursor cursor) {
        String result = "";
        for (int i = 0; i < cursor.getColumnCount(); ++i) {
            result += cursor.getString(i) + "\t";
        }

        return result;
    }

    public void printAll() {
        Log.d(TAG, ProductTable.TABLE_NAME + " count = " + getTableRowsCount(ProductTable.TABLE_NAME));
        printTable(ProductTable.TABLE_NAME);
    }

    public void addProduct(String name, int pid) {
        if (!database.isOpen()) {
            Log.d(TAG, "PROBLEM WITH DB!");
            database = getWritableDatabase();
        }

        ContentValues values = new ContentValues(2);
        values.put(ProductTable.NAME_COLUMN, name);
        values.put(ProductTable.PID_COLUMN, pid);

        database.insert(ProductTable.TABLE_NAME, null, values);
    }

    public void fill() {
        //ProductTable
        addProduct("Рога", 7);
        addProduct("Копыта", 17);
        addProduct("Цветы", 27);
        addProduct("Драгметаллы", 37);
        addProduct("Чай", 38);
        addProduct("Кофе", 39);
        addProduct("Принтер", 40);
        addProduct("МФО", 41);
        for (int i = 50; i < 3050; ++i) {
            addProduct("МультиПродукт " + i, i + 7);
        }
    }

    public Cursor getTableCursor(String tableName) {
        if (!database.isOpen()) {
            Log.d(TAG, "PROBLEM WITH DB!");
            database = getWritableDatabase();
        }

        return database.rawQuery("SELECT * FROM \"" + tableName + "\";", null);
    }

    public void printCountWithTag(String tag) {
        Log.d(TAG, tag+ "=" + getTableRowsCount(ProductTable.TABLE_NAME));
    }

    public void fillProduct(int offset, int count) {
        database.beginTransaction();
        try {
            for (int i = 0; i < count; ++i) {
                ContentValues values = new ContentValues(2);
                values.put(ProductTable.NAME_COLUMN, "Прод " + (offset + i));
                values.put(ProductTable.PID_COLUMN, (offset + i + 7));

                database.insert(ProductTable.TABLE_NAME, null, values);
            }

            database.setTransactionSuccessful();
        } finally {
            database.endTransaction();
        }
    }
}
